package com.horoscope.astrology.zodiac.signs.free.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.horoscope.astrology.zodiac.signs.free.R;

public class ProfileToNameActivity extends BaseActivity {
    private EditText editName;
    private static int RESULT_OK_USER_NAME = 2;
    private static int RESULT_OK_BORN_PLACE = 3;
    private static int FLAG_ITEM = 1;
    private Intent intent;

    public static Intent generateIntent(Context context, String name) {
        Intent intent = new Intent(context, ProfileToNameActivity.class);
        intent.putExtra("ItemName", name);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_name_edit);
        initTitleBar(
                this,
                R.id.btn_back_edit_name,
                R.id.text_view_back_title,
                getString(R.string.label_profile)
        );
        initView();
    }

    private void initView() {
        if (getSupportActionBar() != null)
            getSupportActionBar().hide();
        editName = findViewById(R.id.edit_name);
        if (getIntent().getExtras().getString("ItemName").equals("UserNameEdit")) {
            FLAG_ITEM = 0;
            String userName = getIntent().getExtras().getString("UserName");
            editName.setText(userName);
        }
        if (getIntent().getExtras().getString("ItemName").equals("BornPlaceEdit")) {
            FLAG_ITEM = 3;
            String bornPlace = getIntent().getExtras().getString("BornPlace");
            editName.setText(bornPlace);
        }
        intent = new Intent();

        editName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (FLAG_ITEM == 0) {
                    intent.putExtra("UserName", editName.getText().toString());
                    ProfileToNameActivity.this.setResult(RESULT_OK_USER_NAME, intent);
                } else if (FLAG_ITEM == 3) {
                    intent.putExtra("BornPlace", editName.getText().toString());
                    ProfileToNameActivity.this.setResult(RESULT_OK_BORN_PLACE, intent);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


    }
}
